package controller;

import dao.MedicineDao;
import entity.Goods;
import entity.Medicine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/medicine")
@Scope("prototype")
public class MedicineController {
    @Autowired
    private MedicineDao medicineDao;


    @RequestMapping("/select")
    @ResponseBody
    public Map<String, Object> select(String mName ,int pageNumber, int pageSize){
        Map<String,Object> mapResult = new HashMap<String,Object>();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("offset",(pageNumber-1)*pageSize);
        map.put("limit",pageSize);
        map.put("Name",mName);
        List<Medicine> list = medicineDao.query(map);
        mapResult.put("rows",list);
        mapResult.put("total",medicineDao.getCount());
        return mapResult;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(int medicineId, int pageNumber, int pageSize, HttpServletRequest req){
        System.out.println(medicineId);
        Map<String,Object> mapResult=new HashMap<String, Object>();
        Map<String,Object> map = new HashMap<String, Object>();

        map.put("offset",(pageNumber-1)*pageSize);
        map.put("limit",pageSize);
        map.put("medicineId",medicineId);

        if(medicineDao.delete(medicineId)){
            List<Medicine> list = medicineDao.query(map);
            System.out.println(medicineId+"22");
            mapResult.put("rows",list);
            mapResult.put("total",medicineDao.getCount());
            return mapResult;
        }
        return mapResult;
    }

    @RequestMapping("/queryPage")
    @ResponseBody
    public Map<String,Object> queryPage(@RequestParam(value = "pageNumber",defaultValue = "1",required = false) int pageNumber, @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> mapResult = new HashMap<String,Object>() ;
        map.put("offset",(pageNumber-1)*pageSize) ;
        map.put("limit",pageSize) ;
        List<Medicine> list = medicineDao.queryPage(map);
        mapResult.put("rows",list);
        mapResult.put("total",medicineDao.getCount());
        return mapResult;
    }


    @RequestMapping("/addMedicine")
    @ResponseBody
    public int addMedicine(Medicine medicine, @RequestParam(value = "pageNumber",defaultValue = "1",required = false) int pageNumber, @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize) {
        int i = medicineDao.addMedicine(medicine);
        return i;
    }
}
